<?php
declare (strict_types = 1);

namespace app\controller\backend;

use app\model\Model;
use app\model\WebsiteLang;
use app\model\WebsiteSetting;
use app\validate\WebsiteLangValidate;
use think\exception\ValidateException;


class WebsiteLangController extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\response\Json
     * @throws \app\exception\ModelException
     */
    public function index(WebsiteLang $websiteLang): \think\response\Json
    {
        $siteId = (int)input('site_id');
        if(!$siteId){
            return jsonReturn(-1, '站点ID不能为空');
        }
        $where = [
            'seller_id' => $this->admin['seller_id'],
            'website_id'    => $siteId,
        ];
        $websiteLangList = $websiteLang->getWebsiteLangList($where);
        return json($websiteLangList);
    }

    /**
     * 保存新建的资源
     *
     * @return \think\response\Json
     * @throws \app\exception\ModelException
     */
    public function save(WebsiteLang $websiteLang): \think\response\Json
    {
        if(request()->isPost()){
            $param = input('post.');
            // 数据验证
            try{
                validate(WebsiteLangValidate::class)->scene('save')->check($param);
            }catch(ValidateException $e){
                return jsonReturn(-1, $e->getError());
            }
            $lang = $param['lang'];
            $sysLang = config('system.lang');
            $sysLangKey = array_flip($sysLang);
            $diff = array_diff($lang,$sysLang);
            if(count($diff)){
                return jsonReturn(-2,'语言选择错误');
            }
            $websiteLangs = $websiteLang->getWebsiteLangList(['seller_id'=>$this->admin['seller_id'],'website_id'=>$param['site_id']])['data'];
            $langs = array_column($websiteLangs,'lang');
            $data = [];
            $sysData = [];
            foreach($lang as $key => $val){
                if(in_array($val,$langs)){
                    continue;
                }else{
                    // 增加语言
                    $data[]=[
                        'seller_id' => $this->admin['seller_id'],
                        'website_id'    => $param['site_id'],
                        'lang_name' => $sysLangKey[$val],
                        'lang' => $val,
                    ];
                    // 增加基本配置
                    $tmpData = [
                        "site_name"=>"",
                        "seo_title"=>"",
                        "seo_keywords"=>"",
                        "seo_description"=>"",
                        "icp"=>"",
                        "gwa"=>"",
                        "admin_email"=>"",
                        "analytics_code"=>"",
                        "author"=>"派流",
                        "icp_link"=>"https://beian.miit.gov.cn/#/Integrated/index",
                        "gwa_link"=>"http://www.beian.gov.cn/portal/index.do",
                        "logo"=>"",
                        "customer_code" => "",
                    ];
                    $sysData[] = [
                        "setting"=> json_encode($tmpData),
                        "website_id"=>$param['site_id'],
                        "lang"=> $val
                    ];
                }
            }
            $siteSetting = new WebsiteSetting();
            $siteSetting -> addAllCustomData($sysData);
            $res = $websiteLang -> addWebsiteLang($data);
            return json($res);
        }
        return jsonReturn(-3,'请求方法错误');
    }

    /**
     * 系统语言
     * @return \think\response\Json
     */
    public function system(): \think\response\Json
    {
        if(request()->isGet()){
            $lang = config('system.lang');
            return json(['code'=>0,'msg'=>'success','data'=>$lang]);
        }
        return jsonReturn(-3,'请求方法错误');
    }
}
